package com.guys8.cemsbackend.controller;


import com.guys8.cemsbackend.DTO.HealthInformation;
import com.guys8.cemsbackend.DTO.Response;
import com.guys8.cemsbackend.DTO.UserInformation;
import com.guys8.cemsbackend.entity.Health;
import com.guys8.cemsbackend.entity.User;
import com.guys8.cemsbackend.service.impl.HealthServiceImpl;
import com.guys8.cemsbackend.service.impl.UserServiceImpl;
import com.guys8.cemsbackend.utils.JWTutil.JWTUtil;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * (Health)表控制层
 *
 * @author breeze
 * @since 2022-08-25 09:50:55
 */
@RestController
@Api(tags=" Health-信息相关接口")
@RequestMapping("/Health")
public class HealthController {
    /**
     * 服务对象
     */
    @Autowired
    private HealthServiceImpl healthService;
    @Autowired
    private UserServiceImpl userService;


    @PostMapping("/makeHealth/{bodyTemperature}&&{hcode}")
    @ApiOperation("申报一个健康状况(token)")
    public Response makeHealth(@RequestHeader String schoolid, @PathVariable float bodyTemperature, @PathVariable String hcode) {
        schoolid = JWTUtil.getUsername(schoolid);

        User user = userService.getbyschoolid(schoolid);
        if(user==null) return Response.restResult(0);
        Health health=new Health(user.getUserId(),bodyTemperature,hcode);
        synchronized(this){
            healthService.save(health);
        }
        Health Nhealth = healthService.getusernowHealth(health.getUserId(),health.getUpTime());
        user.setLastHealthid(Nhealth.getHealthId());
        userService.updateById(user);
        return Response.restResult(1);
    }

    @DeleteMapping("/deleteHealth/")
    @ApiOperation("删除最近一次健康状况(token)")
    public Response deleteHealth(@RequestHeader String schoolid) {
        schoolid = JWTUtil.getUsername(schoolid);

        User user = userService.getbyschoolid(schoolid);
        if(user==null) return Response.restResult(0);
        Health health = healthService.getById(user.getLastHealthid());
        if (health != null) {
            healthService.removeById(health.getHealthId());
            return Response.restResult(1);
        } else {
           return Response.restResult(0);
        }

    }

    @PostMapping("/updateHealth/{bodyTemperature}&&{hcode}")
    @ApiOperation("修改最近一次健康状况(token)")
    public Response updateHealth(@RequestHeader String schoolid, @PathVariable float bodyTemperature, @PathVariable String hcode) {
        schoolid = JWTUtil.getUsername(schoolid);

        User user = userService.getbyschoolid(schoolid);
        if(user==null) return Response.restResult(0);
        Health health = healthService.getById(user.getLastHealthid());
        if(health == null) return Response.restResult(0);
        Date uptime=new Date();
        health.setUpTime(uptime);
        health.setBodyTemperature(bodyTemperature);
        health.setHcode(hcode);
        healthService.updateById(health);
        return Response.restResult(1);
    }

    @GetMapping("/getBySchoolid/")
    @ApiOperation("按token查找用户最近一次健康信息成功返回health(token)")
    public Response getInfoByToken(@RequestHeader String schoolid){
        schoolid = JWTUtil.getUsername(schoolid);
        return getInfoByschoolId(schoolid);
    }

    @PostMapping("/getBySchoolid/find/{schoolid}")
    @ApiOperation("按schoolid查找用户最近一次健康信息成功返回health")
    public Response getInfoByschoolId(@PathVariable String schoolid) {
        User user = userService.getbyschoolid(schoolid);
        if(user==null||user.getLastHealthid()==null) return Response.restResult(0);
        Health health = healthService.getById(user.getLastHealthid());
        if(health == null) return Response.restResult(0);
        HealthInformation healthInformation =  new HealthInformation(user,health);
        UserInformation userIn = new UserInformation(user);
        return Response.restResult(1,userIn,healthInformation);
    }

    @GetMapping("/getAllBySchoolid/")
    @ApiOperation("按token查找用户上传的所有健康信息成功返回List<Health>(token)")
    public Response getAllbyToken(@RequestHeader String schoolid){
        schoolid = JWTUtil.getUsername(schoolid);
        return getAllByschoolId(schoolid);
    }

    @PostMapping("/getAllBySchoolid/find/{schoolid}")
    @ApiOperation("按schoolid查找用户上传的所有健康信息成功返回List<Health>")
    public Response getAllByschoolId(@PathVariable String schoolid) {
        User user = userService.getbyschoolid(schoolid);
        if(user==null) return Response.restResult(0);
        List<Health> healths = healthService.getallById(user.getUserId());
        List<HealthInformation> healthss = new LinkedList<>();
        for(Health h:healths){
            healthss.add(new HealthInformation(user,h));
        }
        UserInformation userIn = new UserInformation(user);
        return Response.restResult(1,userIn,healthss);
    }

    @GetMapping("/getById/{healthid}")
    @ApiOperation("按Id查找信息(数据库测试展示接口)")
    public HealthInformation getoneById(@PathVariable Integer healthid) {
        Health health = healthService.getById(healthid);
        User user = userService.getById(health.getUserId());
        return new HealthInformation(user,health);
    }

}

